package controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import model.Company_info;
import model.SelectedSubMenus;
import model.Sys_priv;
import model.Sys_priv_menu;
import model.Sys_sub_menu;
import model.User_list;
import model.V_priv;

import com.et.mvc.JsonView;

public class Sys_privController extends ApplicationController {
	/**
	 * 返回全部角色
	 */
	public JsonView getSysprivs(int rows, int page) throws Exception {
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();
		User_list user = (User_list)session.getAttribute("sessionAccount");
		String company = user.getUser_company();
		tmpArgs.add(company);
		tmpArgs.add(company);
		Object[] args = tmpArgs.toArray();
		int total = V_priv.findBySql(V_priv.class,"select * from v_priv where  company_id=? or(parent_id=? and priv_name like '%admin_%')",args).size();
		List<V_priv> units = V_priv.findBySql(V_priv.class,"select * from v_priv where  company_id=? or(parent_id=? and priv_name like '%admin_%')",args,"priv_id", rows, (page - 1) * rows);
//		long total = V_priv.count(V_priv.class, cond, args); // 查询单位总数
//		List<V_priv> units = V_priv.findAll(V_priv.class,cond, args,"priv_id", rows, (page - 1) * rows); // 查询单位的一页资料
		// 构造JSON用的数据结构并返回JSON视图
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total);
		result.put("rows", units);
		return new JsonView(result);
	}
	//功能分配
	public JsonView getSys_privs(int rows, int page, Integer id) throws Exception {
		User_list user = (User_list)session.getAttribute("sessionAccount");
		String company = user.getUser_company();
		String cond1 = "SELECT spm.* FROM sys_priv_menu spm INNER JOIN sys_priv sp ON spm.priv_no=sp.id AND sp.company_id="+company+" where 1=1 ";
		List<Object> tmpArgs1 = new ArrayList<Object>();
		if (id != null && !id.equals("")) {
			cond1 += " and spm.id like ?";
			tmpArgs1.add("%" + id + "%");
		}
		Object[] args1 = tmpArgs1.toArray();
		long total1 = Sys_priv_menu.count(Sys_priv_menu.class, cond1, args1);
		List<Sys_priv_menu> Sys_priv_menus = Sys_priv_menu.findBySql(Sys_priv_menu.class, cond1,
				args1);

		// 查询所有的功能
		String cond2 = "SELECT ssm.* FROM sys_priv_menu spm INNER JOIN sys_priv sp ON spm.priv_no=sp.id AND sp.company_id="+company+" INNER JOIN sys_sub_menu ssm ON spm.sys_sub_menu_id=ssm.id";
		List<Object> tmpArgs2 = new ArrayList<Object>();
		Object[] args2 = tmpArgs2.toArray();
		long total2 = Sys_sub_menu.count(Sys_sub_menu.class, cond2, args2); // 查询总数
		List<Sys_sub_menu> sub_menus = Sys_sub_menu.findBySql(Sys_sub_menu.class,
				cond2, args2, "id", rows, (page - 1) * rows); // 查询的一页资料

		List<SelectedSubMenus> ssms = new ArrayList<SelectedSubMenus>();

		for (Sys_sub_menu sms : sub_menus) {
			SelectedSubMenus ssm = new SelectedSubMenus();
			ssm.setCk("");
			for (Sys_priv_menu rms : Sys_priv_menus) {
				if (sms.getId().equals(rms.getSys_sub_menu_id())) {
					ssm.setCk("已选");// 有该功能
				}
			}
			ssm.setFunc_css(sms.getFunc_css());
			ssm.setFunc_name(sms.getFunc_name());
			ssm.setFunc_url(sms.getFunc_url());
			ssm.setId(sms.getId());
			ssm.setImage(sms.getImage());
			ssm.setSys_menuId(sms.getSys_menuId());
			ssm.setTab_name(sms.getTab_name());

			// 将selectedsubmenu对象放到list中
			ssms.add(ssm);
		}
		// 构造JSON用的数据结构并返回JSON视图
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", total2);
		result.put("rows", ssms);
		return new JsonView(result);
	}
	public JsonView getSys_priv(int id) throws Exception {
		Sys_priv unit = Sys_priv.find(Sys_priv.class, id);
		return new JsonView(unit);
	}

	/**
	 * 保存新建角色
	 */
	public JsonView save(Sys_priv Sys_priv) throws Exception {
		if(Sys_priv.getCompany_id()==null || Sys_priv.getCompany_id().length()<=0){
			User_list user = (User_list)session.getAttribute("sessionAccount");
			String company = user.getUser_company();
			Sys_priv.setCompany_id(company);
		}
		Sys_priv.save();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}

	/**
	 * 保存修改的角色
	 */
	public JsonView update(int id) throws Exception {
		Sys_priv unit = Sys_priv.find(Sys_priv.class, id);
		
		updateModel(unit);
		unit.save();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}

	/**
	 * 删除指定的角色
	 */
	public JsonView destroy(int id) throws Exception {
		Sys_priv unit = Sys_priv.find(Sys_priv.class, id);
		unit.destroy();
		JsonView view = new JsonView("success:true");
		view.setContentType("text/html;charset=utf-8");
		return view;
	}
	public JsonView getCompanys()throws Exception{
		String condition = "company_id!=0";
		List<Object> tmpArgs = new ArrayList<Object>();
		Object[] args = tmpArgs.toArray();
		List<Company_info> list = Company_info.findAll(Company_info.class,condition,args);
		return new JsonView(list);
	}
}
